import * as $group from '@/api/group'

export const GROUP_SET_MEMBERS = 'GROUP_SET_MEMBERS'
export const GROUP_TOGGLE_CHECK = 'GROUP_TOGGLE_CHECK'
export const GROUP_SET_CHECK_ALL = 'GROUP_SET_CHECK_ALL'
export const GROUP_SET_GROUP = 'GROUP_SET_GROUP'
export const GROUP_SET_GROUPS = 'GROUP_SET_GROUPS'
export const GROUP_UNSHIFT_GROUPS = 'GROUP_UNSHIFT_GROUPS'
export const GROUP_SET_SELECTED_GROUP_NAMES = 'GROUP_SET_SELECTED_GROUP_NAMES'

const state = () => {
  return {
    members: [],
    group: {
      id: 0,
      name: '',
      owner: {
        nickName: ''
      }
    },
    groups: [],
    selectedGroupNames: ''
  }
}

const getters = {
  members (state) {
    let members = state.members
    for (let i = 0; i < members.length; i++) {
      if (members[i].checked === 'undefined') {
        members[i].checked = false
      }
    }
    return members
  },
  groups: state => state.groups,
  selectedGroupNames: state => state.selectedGroupNames,
  group (state, gettters, rootState, rootGetters) {
    let group = state.group
    let members = group.members || []
    let loginer = rootGetters['loginer']
    let t = []
    members.forEach(n => {
      if (n.userId === loginer.id) t.push(n)
    })
    members.forEach(n => {
      if (n.userId !== loginer.id) t.push(n)
    })
    group.members = t
    return group
  }
}

const actions = {
  async getMembersById ({ commit, state, rootState }, groupId) {
    let members = await $group.getMembersById(groupId)
    commit(GROUP_SET_MEMBERS, members)
    return members
  },
  async getById ({ commit, state, rootState }, {groupId, cache = true}) {
    let group = await $group.getById({groupId, cache})
    if (group) {
      commit(GROUP_SET_GROUP, group)
    }
    return group
  },
  async getMyGroups ({ commit, state, rootState }) {
    let groups = await $group.getMys()
    commit(GROUP_SET_GROUPS, groups)
    return groups
  },
  async getMyRelates ({ commit, state, rootState }) {
    let groups = await $group.getMyRelates()
    commit(GROUP_SET_GROUPS, groups)
    return groups
  },
  async getMyGroupInfo ({ commit, state, rootState }, groupId) {
    let group = await $group.getMyGroupInfo(groupId)
    return group
  },
  toggleCheck ({ commit, state, rootState }, idx) {
    commit(GROUP_TOGGLE_CHECK, idx)
  },
  setCheckAll ({ commit, state, rootState }, value) {
    commit(GROUP_SET_CHECK_ALL, value)
  },
}

const mutations = {
  GROUP_SET_MEMBERS (state, members) {
    state.members = members
  },
  GROUP_SET_SELECTED_GROUP_NAMES (state, names) {
    state.selectedGroupNames = names
  },
  GROUP_SET_GROUP (state, group) {
    state.group = group
  },
  GROUP_SET_GROUPS (state, groups) {
    state.groups = groups
  },
  GROUP_UNSHIFT_GROUPS (state, group) {
    state.groups = [group, ...state.groups]
  },
  GROUP_TOGGLE_CHECK (state, idx) {
    if (!state.members[idx]) return
    let t = []
    state.members.forEach((n, i) => {
      if (i === idx) {
        n.checked = !n.checked
      }
      t.push(n)
    })
    state.members = t
  },
  GROUP_SET_CHECK_ALL (state, value) {
    let t = []
    state.members.forEach((n, i) => {
      n.checked = value
      t.push(n)
    })
    state.members = t
  },
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
